package com.voicechat.firstmet.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseFragment;
import com.voicechat.firstmet.imcontrol.BroadcastManager;
import com.voicechat.firstmet.view.LazyViewPager;
import com.voicechat.firstmet.view.SimpleViewPagerIndicator;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 语聊
 * Created by Administrator on 2018/8/28.
 */

public class VoiceChatFragment extends BaseFragment {


    @BindView(R.id.invest_indicator)
    SimpleViewPagerIndicator investIndicator;
    @BindView(R.id.invest_list)
    LazyViewPager investList;
    private String[] mTitles;
    private ArrayList<Fragment> fragments;
    private MyAdapter mAdapter;
    private String TAG = "VoiceChatFragment";

    @Override
    protected int loadLayout() {
        return R.layout.fragment_voicechat;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        initView();
    }

    /**
     * 数据实例化
     */
    private void initView() {
        mTitles = getResources().getStringArray(R.array.voice_chat_list);
        initIndicator();
        bindEvent();
        initViewPager();
    }
    private void initViewPager() {
        fragments = new ArrayList<>();
        fragments.add(VoiceChatListFragment.newInstance("ALL"));
        fragments.add(VoiceChatListFragment.newInstance("POPU"));
        fragments.add(VoiceChatListFragment.newInstance("NEW"));
        /*fragments.add(VoiceChatListFragment.newInstance("CITY"));*/
        mAdapter = new MyAdapter(getChildFragmentManager());
        investList.setAdapter(mAdapter);
    }
    private void bindEvent() {
        investIndicator.setOnPageChangeListener(new SimpleViewPagerIndicator.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                investIndicator.setCurrentItem(position);
            }
        });
    }
    private void initIndicator() {
        if (null == mTitles || mTitles.length == 0) {
            investIndicator.setVisibility(View.GONE);
        } else {
            investIndicator.setTitles(mTitles);
            investIndicator
                    .setViewPager(investList);
            investList.setCurrentItem(0);
        }
    }
    private class MyAdapter extends FragmentStatePagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        @Override
        public Fragment getItem(int arg0) {
            // TODO Auto-generated method stub
            return fragments.get(arg0);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return fragments.size();
        }

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden){
            BroadcastManager.getInstance(activity).sendBroadcast("hidden");
        }
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

}
